<?php

namespace app\api\controller;

use app\admin\model\article\OnlineVideo;
use app\admin\model\article\Works;
use app\admin\model\user\TeacherCategory;
use app\admin\model\UserCollection;
use app\admin\model\article\WorksCategory;
use app\common\controller\Api;

/**
 * 内容模块
 * @ApiWeigh (30)
 */
class Content extends Api
{
    protected $noNeedLogin = ["getWorksCategory","getWorksList","getWorksInfo","getOnlineVideoList","getOnlineVideoInfo","getTeacherCategory","getTeacherList","getTeacherInfo"];
    protected $noNeedRight = ['*'];

    /**
     * 获取优秀作品分类
     * @ApiMethod   (POST)
     * @ApiWeigh (80)
     * @ApiReturnParams   (name="name", type="string", required=true, description="名称")
     */
    public function getWorksCategory(){
        $worksCategoryModel = new TeacherCategory();
        $worksModel = new Works();
        $category_ids = $worksModel->where("switch" , 1)->column("category_id");
        try {
            $condition = [
                "id" => ["in",$category_ids]
            ];
            $info = $worksCategoryModel->getList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $info);
    }

    /**
     * 获取优秀作品列表
     * @ApiMethod   (POST)
     * @ApiWeigh (79)
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     * @ApiParams   (name="category_id", type="int", required=true, description="分类id")
     */
    public function getWorksList(){
        $worksModel = new Works();
        $category_id = $this->request->post("category_id");
        try {
            $condition = [

            ];
            if($category_id){
                $condition['category_id'] = $category_id;
            }
            $list = $worksModel->getList($condition,1);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $list);
    }

    /**
     * 获取优秀作品详情
     * @ApiMethod   (POST)
     * @ApiWeigh (78)
     * @ApiParams   (name="id", type="int", required=true, description="作品id")
     *
     * @ApiReturnParams   (name="images", type="array", required=true, description="作品图片")
     * @ApiReturnParams   (name="author", type="string", required=true, description="作者")
     * @ApiReturnParams   (name="teacher", type="string", required=true, description="指导老师")
     * @ApiReturnParams   (name="author_content", type="string", required=true, description="作者介绍")
     * @ApiReturnParams   (name="is_collection", type="bool", required=true, description="是否收藏")
     */
    public function getWorksInfo(){
        $worksModel = new Works();
        $id = $this->request->post("id");
        try {
            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $worksModel->getInfo($condition,$this->auth->getUser());
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $info);
    }

    /**
     * 收藏优秀作品
     * @ApiMethod   (POST)
     * @ApiWeigh (77)
     * @ApiParams   (name="id", type="int", required=true, description="作品id")
     *
     */
    public function collectionWorks(){
        $worksModel = new Works();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $worksModel->getInfo($condition,$this->auth->getUser());
            $userCollectionModel->collectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_WORKS);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("收藏成功");
    }

    /**
     * 取消收藏优秀作品
     * @ApiMethod   (POST)
     * @ApiWeigh (76)
     * @ApiParams   (name="id", type="int", required=true, description="作品id")
     *
     */
    public function cancelCollectionWorks(){
        $worksModel = new Works();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $worksModel->getInfo($condition,$this->auth->getUser());
            $userCollectionModel->cancelCollectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_WORKS);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("取消成功");
    }
    /**
     * 收藏优秀作品列表
     * @ApiMethod   (POST)
     * @ApiWeigh (76)
     *
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     *
     */
    public function collectionWorksList(){
        try {
            $userCollectionModel = new UserCollection();
            $list = $userCollectionModel->collectionInfoList($this->auth->getUser(),$userCollectionModel::TYPE_WORKS);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }

    /**
     * 获取线上视频列表
     * @ApiMethod   (POST)
     * @ApiWeigh (70)
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     */
    public function getOnlineVideoList(){
        $onlineVideoModel = new OnlineVideo();
        try {
            $condition = [
            ];

            $list = $onlineVideoModel->getList($condition,1);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $list);
    }

    /**
     * 获取线上视频详情
     * @ApiMethod   (POST)
     * @ApiWeigh (69)
     * @ApiParams   (name="id", type="int", required=true, description="视频id")
     *
     * @ApiReturnParams   (name="author", type="string", required=true, description="作者")
     * @ApiReturnParams   (name="image", type="string", required=true, description="封面图")
     * @ApiReturnParams   (name="video_file", type="string", required=true, description="视频")
     * @ApiReturnParams   (name="video_content", type="string", required=true, description="课程介绍")
     * @ApiReturnParams   (name="is_collection", type="bool", required=true, description="是否收藏")
     */
    public function getOnlineVideoInfo(){
        $onlineVideoModel = new OnlineVideo();
        $id = $this->request->post("id");
        try {

            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $onlineVideoModel->getInfo($condition,$this->auth->getUser());

        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$info);
    }


    /**
     * 收藏线上视频
     * @ApiMethod   (POST)
     * @ApiWeigh (68)
     * @ApiParams   (name="id", type="int", required=true, description="视频id")
     *
     */
    public function collectionOnlineVideo(){
        $onlineVideoModel = new OnlineVideo();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $onlineVideoModel->getInfo($condition,$this->auth->getUser());
            $userCollectionModel->collectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_ONLINE_VIDEO);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("收藏成功");
    }

    /**
     * 取消收藏线上视频
     * @ApiMethod   (POST)
     * @ApiWeigh (67)
     * @ApiParams   (name="id", type="int", required=true, description="视频id")
     *
     */
    public function cancelCollectionOnlineVideo(){
        $onlineVideoModel = new OnlineVideo();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
                "switch" => 1,
            ];
            $info = $onlineVideoModel->getInfo($condition,$this->auth->getUser());
            $userCollectionModel->cancelCollectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_ONLINE_VIDEO);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("取消成功");
    }

    /**
     * 收藏线上视频列表
     * @ApiMethod   (POST)
     * @ApiWeigh (66)
     *
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     *
     */
    public function collectionOnlineVideoList(){
        try {
            $userCollectionModel = new UserCollection();
            $list = $userCollectionModel->collectionInfoList($this->auth->getUser(),$userCollectionModel::TYPE_ONLINE_VIDEO);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }

    /**
     * 获取老师分类
     * @ApiMethod   (POST)
     * @ApiWeigh (60)
     * @ApiReturnParams   (name="name", type="string", required=true, description="名称")
     */
    public function getTeacherCategory(){
        $teacherCategoryModel = new TeacherCategory();
        try {
            $condition = [
            ];
            $info = $teacherCategoryModel->getList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $info);
    }

    /**
     * 获取老师列表
     * @ApiMethod   (POST)
     * @ApiWeigh (59)
     * @ApiParams   (name="category_id", type="string", required=true, description="分类id")
     * @ApiReturnParams   (name="nickname", type="string", required=true, description="姓名")
     * @ApiReturnParams   (name="avatar", type="string", required=true, description="头像")
     * @ApiReturnParams   (name="bio", type="string", required=true, description="介绍")
     */
    public function getTeacherList(){
        $userModel = new \app\common\model\User();
        $category_id = $this->request->post("category_id");
        try {
            $condition = [
                "user_type" => 2,
            ];
            if($category_id){
                $condition["works_category_id"] = $category_id;
            }

            $list = $userModel->getTeacherList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $list);
    }

    /**
     * 获取老师详情
     * @ApiMethod   (POST)
     * @ApiWeigh (58)
     * @ApiParams   (name="id", type="string", required=true, description="老师id")
     *
     * @ApiReturnParams   (name="nickname", type="string", required=true, description="姓名")
     * @ApiReturnParams   (name="avatar", type="string", required=true, description="头像")
     * @ApiReturnParams   (name="bio", type="string", required=true, description="介绍")
     * @ApiReturnParams   (name="works_images", type="string", required=true, description="作品")
     * @ApiReturnParams   (name="honor_images", type="string", required=true, description="荣誉证书")
     */
    public function getTeacherInfo(){
        $userModel = new \app\common\model\User();
        $id = $this->request->post("id");
        try {
            $condition = [
                "id" => $id,
            ];
            $info = $userModel->getTeacherInfo($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $info);
    }
}
